ngx.header.content_type="application/json;charset=utf8"
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
    ngx.say("failed to instantiate mysql: ", err)
    return
end
db:set_timeout(1000) -- 1 sec

local ok, err, errcode, sqlstate = db:connect{
    host = "localhost",
    port = 3306,
    database = "database",
    user = "root",
    password = "password",
    max_packet_size = 1024 * 1024 }

if not ok then
    ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
    return
end


res, err, errcode, sqlstate =
    db:query("select roleid,rolename from dh_admin_role limit 10", 10)
if not res then
    ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
    return
end
--[[for i, row in ipairs(res) do  
   for name, value in pairs(row) do  
     ngx.say("select row ", i, " : ", name, " = ", value, "<br/>")  
   end  
end
]]
local cjson = require "cjson"
ngx.say(cjson.encode(res))
